#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2014 OpenWrt.org

START=16
STOP=16
USE_PROCD=1
#使用procd启动


# start启动服务
# stop停止服务
# restart重新启动服务
# reload重新加载配置文件（如果服务未实现重新加载，则重新启动）
# enable启用服务自动启动
# disable禁用服务自动启动
# enabled检查服务是否在启动时启动
# 正在运行检查服务是否正在运行
# status服务状态
# trace从系统调用跟踪开始

runModem()
{
    local enabled
    config_get_bool enabled $1 enabled
    echo "run runModem" >> /tmp/log4g
    if [ "$enabled" = "1" ]; then


        local user
        local password
        local apn
        local auth
        local pincode
        local device
        local tool
        local pdptype


        config_get user $1 user
        config_get password $1 password
        config_get apn $1 apn
        config_get auth $1 auth
        config_get pincode $1 pincode
        config_get device $1 device
        config_get tool $1 tool
        config_get pdptype $1 pdptype
        config_get tty $1 tty
        config_get atcmd $1 atcmd

        if [ "$pdptype" = "IPV4V6" ]; then
            pdptype='-4 -6'
        elif [ "$pdptype" = "IPV6" ]; then
            pdptype='-6'
        else
             pdptype=''
        fi

        devname="$(basename "$device")"
        devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
        ifname="$( ls "$devpath"/net )"


        procd_open_instance
        #创建一个实例， 在procd看来一个应用程序可以多个实\E4\BE?
        #ubus call service list 可以查看实例
        procd_set_param command $tool -i $ifname -s $apn $pdptype
        if [ "$password" != "" ];then
            procd_append_param command $user $password $auth
        fi
        if [ "$pincode" != "" ]; then
            procd_append_param command -p $pincode
        fi
        # procd_append_param command -f /tmp/4g.log
        procd_set_param respawn
        echo "quectel-CM has started."
        procd_close_instance
        #关闭实例
    fi


    
}




service_triggers()
{
	procd_add_reload_trigger "modem"
}

start_service() {
	config_load modem
	config_foreach runModem ndis
}

stop_service()
{
    echo "runModem stop" >> /tmp/log4g
    killall quectel-CM
    echo "quectel-CM has stoped."
}

